package com.bdsys.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bdsys.dao.DeviceDAO;

/**
 * Servlet implementation class DeviceCodeExistServlet
 */
public class DeviceCodeExistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private static BeanFactory applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
	
	private static DeviceDAO devicedao = (DeviceDAO)applicationContext.getBean("deviceDAO");
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeviceCodeExistServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String code = request.getParameter("code");
		List<String> devicesCode = devicedao.selectAllDeviceCode();
		PrintWriter out=response.getWriter();
		   
		if(devicesCode.contains(code)){
			out.println("<response>exsit</response>"); 
		}else {
			out.println("<response>noexsit</response>");
		}
		
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
